﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AddIn.EA.Architecture.ComponentLogic.ElementModel;

namespace AddIn.EA.Architecture.ComponentLogic.StandardProject.Builders.UI
{
    public class ElementBuilderCommunication : ElementBuilder
    {
        public override void BuildStructure(ComponentLogic.ElementModel.Package viewModel, Framework.RepositoryManager templateRepository, BusinessEntityStandardProject itemEntity)
        {
            BuildCommunicationElement(viewModel, templateRepository, itemEntity);
            BuildTranslatorModelToDataContractElement(viewModel, templateRepository, itemEntity);
            BuildTranslatorDataContractToModelElement(viewModel, templateRepository, itemEntity);
        }
        public void BuildCommunicationElement(ComponentLogic.ElementModel.Package viewModel, Framework.RepositoryManager templateRepository, BusinessEntityStandardProject itemEntity)
        {
            AddIn.EA.Framework.Package templatePackage = templateRepository.GetPackageByName("UI").GetPackageByName("Communication");
            AddIn.EA.Framework.Element templateCommunicationElement = templatePackage.GetElementByName("[entity_name]");

            //Setup the BusinessEntity package
            ComponentLogic.ElementModel.Package uiPackage = GetViewModelPackage("UI", viewModel, true);
            ComponentLogic.ElementModel.Package communicationPackage = GetViewModelPackage("Communication", uiPackage, true);

            String elementName = templateCommunicationElement.Name.Replace("[entity_name]", itemEntity.Name);

            //Prevent existing item before include
            communicationPackage.Elements.RemoveAll(it => it.Name.CompareTo(elementName) == 0);

            //Build BusinessEntity attributes.
            Element communicationElement = BuildEntityElementFromTemplate(itemEntity, templateCommunicationElement, elementName);
            communicationElement.Attributes.ForEach(it => it.Type = it.Type.Replace("List<", "ModelCollection<"));

            //Add the methods defined for this layer.
            BindElementMethods(communicationElement, itemEntity, "Model", "Model");
            foreach (ElementModel.Method itemMethod in communicationElement.Methods)
            {
                itemMethod.Parameters = itemMethod.Parameters.Replace("List<", "ModelCollection<");
                itemMethod.Return = itemMethod.Return.Replace("List<", "ModelCollection<");
            }

            communicationPackage.Elements.Add(communicationElement);
        }

        public void BuildTranslatorModelToDataContractElement(ComponentLogic.ElementModel.Package viewModel, Framework.RepositoryManager templateRepository, BusinessEntityStandardProject itemEntity)
        {
            AddIn.EA.Framework.Package templatePackage = templateRepository.GetPackageByName("UI").GetPackageByName("Communication").GetPackageByName("Translator").GetPackageByName("ModelToDataContract");
            AddIn.EA.Framework.Element templateCommunicationElement = templatePackage.GetElementByName("[entity_name]");

            //Setup the BusinessEntity package
            ComponentLogic.ElementModel.Package uiPackage = GetViewModelPackage("UI", viewModel, true);
            ComponentLogic.ElementModel.Package communicationPackage = GetViewModelPackage("Communication", uiPackage, true);
            ComponentLogic.ElementModel.Package translatorPackage = GetViewModelPackage("Translator", communicationPackage, true);
            ComponentLogic.ElementModel.Package translatorModelToDataContractPackage = GetViewModelPackage("ModelToDataContract", translatorPackage, true);

            String elementName = templateCommunicationElement.Name.Replace("[entity_name]", itemEntity.Name);

            //Prevent existing item before include
            translatorModelToDataContractPackage.Elements.RemoveAll(it => it.Name.CompareTo(elementName) == 0);

            Element communicationElement = BuildEntityElementFromTemplate(itemEntity, templateCommunicationElement, elementName);

            translatorModelToDataContractPackage.Elements.Add(communicationElement);            
        }


        public void BuildTranslatorDataContractToModelElement(ComponentLogic.ElementModel.Package viewModel, Framework.RepositoryManager templateRepository, BusinessEntityStandardProject itemEntity)
        {
            AddIn.EA.Framework.Package templatePackage = templateRepository.GetPackageByName("UI").GetPackageByName("Communication").GetPackageByName("Translator").GetPackageByName("DataContractToModel");
            AddIn.EA.Framework.Element templateCommunicationElement = templatePackage.GetElementByName("[entity_name]");

            //Setup the BusinessEntity package
            ComponentLogic.ElementModel.Package uiPackage = GetViewModelPackage("UI", viewModel, true);
            ComponentLogic.ElementModel.Package communicationPackage = GetViewModelPackage("Communication", uiPackage, true);
            ComponentLogic.ElementModel.Package translatorPackage = GetViewModelPackage("Translator", communicationPackage, true);
            ComponentLogic.ElementModel.Package translatorDataContractToModelPackage = GetViewModelPackage("DataContractToModel", translatorPackage, true);

            String elementName = templateCommunicationElement.Name.Replace("[entity_name]", itemEntity.Name);

            //Prevent existing item before include
            translatorDataContractToModelPackage.Elements.RemoveAll(it => it.Name.CompareTo(elementName) == 0);

            Element communicationElement = BuildEntityElementFromTemplate(itemEntity, templateCommunicationElement, elementName);

            translatorDataContractToModelPackage.Elements.Add(communicationElement);
        }

        public override void Validate()
        {
        }
    }
}
